//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_CV_FEATURES_CFEATUREMATCHES_H_
#define _BUOLA_CV_FEATURES_CFEATUREMATCHES_H_

#include <buola/cv/features/cfeatureset.h>
#include <buola/cv/features/cmatch.h>

namespace buola { namespace cvi {

class CFeatureMatches
{
    struct SData : public UShared
    {
        SData()=default;
        
        SData(const SData &pData)
            :   UShared()
            ,   mSet1(share(pData.mSet1))
            ,   mSet2(share(pData.mSet2))
            ,   mMatches(pData.mMatches)
        {}
        
        CFeatureSet mSet1,mSet2;
        std::vector<CMatch> mMatches;
    };
    
public:
    CFeatureMatches(const CFeatureSet &pSet1,const CFeatureSet &pSet2,std::vector<CMatch> &&pMatches)
        :   mData(new SData)
    {
        mData->mSet1=share(pSet1);
        mData->mSet2=share(pSet2);
        mData->mMatches=std::move(pMatches);
    }
    
    CFeatureMatches()
        :   mData(nullptr)
    {}

    CFeatureMatches(const CFeatureMatches &pRH)=delete;

    CFeatureMatches(CFeatureMatches &&pRH)
        :   mData(pRH.mData)
    {
        pRH.mData=nullptr;
    }
    
    CFeatureMatches(NClone,const CFeatureMatches &pRH)
        :   mData(new SData(*pRH.mData))
    {}

    CFeatureMatches(NShare,const CFeatureMatches &pRH)
        :   mData(pRH.mData)
    {}

    ~CFeatureMatches()
    {
    }

    CFeatureMatches &operator=(const CFeatureMatches &pRH)=delete;

    CFeatureMatches &operator=(CFeatureMatches &&pRH)
    {
        if(pRH.mData!=mData)
            std::swap(mData,pRH.mData);
        return *this;
    }

    std::size_t size() const                            {   return mData->mMatches.size();  }
    const CMatch &operator[](std::size_t i) const       {   return mData->mMatches[i];      }
    
    void Sort()                                         {   std::sort(mData->mMatches.begin(),mData->mMatches.end()); }

    const CKeyPoint &KeyPoint1(std::size_t i) const     {   return mData->mSet1[mData->mMatches[i].mIndex1];    }
    const CKeyPoint &KeyPoint2(std::size_t i) const     {   return mData->mSet2[mData->mMatches[i].mIndex2];    }
    
    explicit operator bool()                            {   return (bool)mData;             }
    
private:
    intrusive_ptr<SData> mData;
};

/*namespace cv*/ } /*namespace buola*/ }

#endif
